const deleteAndEarn = nums => {
  let max = 0
  for (const x of nums) {
    max = Math.max(x, max)
  }
  let arr = new Array(max + 1).fill(0)
  for (const x of nums) {
    arr[x] += x
  }
  return rob(arr)
};

const rob = nums => {
  const length = nums.length
  if (length === 1) return nums[0]
  let sum1 = nums[0],
    sum2 = Math.max(nums[0], nums[1])
  for (let i = 2; i < length; i++) {
    let temp = Math.max(sum1 + nums[i], sum2)
    sum1 = sum2
    sum2 = temp
  }
  return sum2
}


console.log(deleteAndEarn([3, 4, 2]));